package com.suarte.webapp.action;

import com.suarte.core.*;
import com.suarte.core.Invoice;
import com.suarte.core.service.ContactManager;
import com.suarte.core.service.InvoiceManager;
import com.suarte.core.service.QuotationManager;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.appfuse.service.GenericManager;
import org.appfuse.model.User;
import javax.servlet.http.HttpServletRequest;

/**
 * @date   Feb 15, 2011
 * @author Ggutierrez
 */
public class InvoiceForm extends BasePage implements Serializable {

    private InvoiceManager invoiceManager;
    private QuotationManager quotationManager;
    private GenericManager<Company, Long> companyManager;
    private ContactManager contactManager;
    private Long id;
    private Invoice invoice;
    private List<Contact> contacts;
    private List<Company> companies;
    private User requestUser;

    public InvoiceForm() {
    }

    public void setInvoiceManager(InvoiceManager invoiceManager) {
        this.invoiceManager = invoiceManager;
    }

    public void setQuotationManager(QuotationManager quotationManager) {
        this.quotationManager = quotationManager;
    }

    public void setCompanyManager(GenericManager<Company, Long> companyManager) {
        this.companyManager = companyManager;
    }

    public void setContactManager(ContactManager contactManager) {
        this.contactManager = contactManager;
    }

    public String edit() {
        contacts = contactManager.getAll();
        companies = companyManager.getAll();

        if (id != null) {
            invoice = invoiceManager.get(id);
        } else {
            invoice = new Invoice();
        }

        return "edit";
    }

    public String cancel() {
        String key = "invoice.canceled";

        invoice.setStatus(DocumentStatus.CANCELADA);
        invoiceManager.save(invoice);

        if (invoice.getCurrency().getSymbol().equals("$")) {
            invoice.getCompany().credit(invoice.getTotal(), invoice.getEquivalentAmount());
        } else if (invoice.getCurrency().getSymbol().equals("C$")) {
            invoice.getCompany().credit(invoice.getEquivalentAmount(), invoice.getTotal());
        }

        companyManager.save(invoice.getCompany());

        invoice.getQuotation().setStatus(QuotationStatus.APROBADA);
        quotationManager.save(invoice.getQuotation());
        addMessage(key);

        return "edit";
    }

    public String save() {
        String key = "invoice.updated";
        HttpServletRequest request = getRequest();
        Date date = new Date();
        requestUser = userManager.getUserByUsername(request.getRemoteUser());

        invoice.setModifiedBy(requestUser);
        invoice.setModifiedOn(date);
        invoiceManager.save(invoice);
        addMessage(key);
        
        return "edit";
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Invoice getInvoice() {
        return invoice;
    }

    public void setInvoice(Invoice invoice) {
        this.invoice = invoice;
    }

    public List<Company> getCompanies() {
        return companies;
    }

    public List<Contact> getContacts() {
        return contacts;
    }

    public void setContacts(List<Contact> contacts) {
        this.contacts = contacts;
    }

    public void setCompanies(List<Company> companies) {
        this.companies = companies;
    }

    public User getRequestUser() {
        return requestUser;
    }

    public void setRequestUser(User requestUser) {
        this.requestUser = requestUser;
    }
}
